﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TasksModel
{
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://mp.tasks.com/2010/")]
    public abstract class IdentifiedObject : System.ComponentModel.INotifyPropertyChanged
    {
        /// <summary>
        /// Not displayed. Do not change from outside constructor. Useful for synchronization.
        /// </summary>
        public Guid ID { get; set; }

        /// <summary>
        /// Increased on each property change.
        /// Do not change manually except in copying constructor.
        /// Useful for synchronization.
        /// </summary>
        public int Timestamp { get; set; }

        /// <summary>
        /// Get schema identifier of instance. Useful for synchronization.
        /// </summary>
        public string Uri
        {
            [System.Diagnostics.DebuggerStepThrough]
            get { return BaseSchema + "id#" + ID.ToString("D"); }
        }

        public static readonly String BaseSchema = "http://mp.tasks.com/2010/";

        [System.Diagnostics.DebuggerStepThrough]
        public override bool Equals(object obj)
        {
            IdentifiedObject o = obj as IdentifiedObject;
            return o != null && o.ID == ID;
        }

        [System.Diagnostics.DebuggerStepThrough]
        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }

        [System.Diagnostics.DebuggerStepThrough]
        public void ForcePropertyChanged()
        {
            NotifyPropertyChanged(null);
        }

        [System.Diagnostics.DebuggerStepThrough]
        protected void NotifyPropertyChanged(string propertyName)
        {
            Timestamp++;
            if (!_disallowEvent)
            {
                var pc = PropertyChanged;
                if (pc != null)
                    pc(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

        //[System.Runtime.TargetedPatchingOptOut("Performance critical")]
        protected static DateTime? CutMilliseconds(DateTime? value)
        {
            if (!value.HasValue)
                return null;
            System.Diagnostics.Debug.Assert(value.Value.Kind != DateTimeKind.Local, "All used times have to be in UTC");
            return new DateTime(
                value.Value.Ticks - (value.Value.Ticks % TimeSpan.TicksPerSecond), DateTimeKind.Utc);
        }

        //[System.Runtime.TargetedPatchingOptOut("Performance critical")]
        protected static DateTime CutMilliseconds(DateTime value)
        {
            System.Diagnostics.Debug.Assert(value.Kind != DateTimeKind.Local, "All used times have to be in UTC");
            return new DateTime(
                value.Ticks - (value.Ticks % TimeSpan.TicksPerSecond), DateTimeKind.Utc);
        }

        private bool _disallowEvent;

        [System.Diagnostics.DebuggerStepThrough]
        //[System.Runtime.TargetedPatchingOptOut("Performance critical")]
        public void SuspendChangedEvent()
        {
            _disallowEvent = true;
        }

        [System.Diagnostics.DebuggerStepThrough]
        //[System.Runtime.TargetedPatchingOptOut("Performance critical")]
        public void ResumeChangedEvent()
        {
            _disallowEvent = false;
        }

        #region INotifyPropertyChanged Members

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        #endregion

        internal static Guid GetAutoStaticGuid(int entity, int index)
        {
            byte[] a = BitConverter.GetBytes((long)0x0000000A);
            byte[] b = BitConverter.GetBytes(entity);
            byte[] c = BitConverter.GetBytes(index);
            byte[] d = new byte[16];
            Array.Copy(a, 0, d, 0, a.Length);
            Array.Copy(b, 0, d, 8, b.Length);
            Array.Copy(c, 0, d, 12, c.Length);
            return new Guid(d);
        }
    }
}